﻿Imports GemBox.Spreadsheet
Imports System
Imports System.IO.Ports
Imports System.Threading
Imports System.Threading.Thread

Public Class Form1
    Dim SpaceCount As Byte = 0
    Dim LookUpTable As String = "0123456789ABCDEF"
    Dim RXArray(2047) As Char ' Text buffer. Must be global to be accessible from more threads.
    Dim RXCnt As Integer      ' Length of text buffer. Must be global too.
    Dim BusId As String
    Dim BId As Integer = 0
    Dim BusDis As String
    Dim DId As Integer = 0
    Dim code(4) As String
    Dim CId As Integer = 0
    Dim flag As Boolean = False
    Dim info As Boolean = False
    Dim ads As Boolean = False

    ' Make a new System.IO.Ports.SerialPort instance, which is able to fire events.
    Dim WithEvents COMPort As New SerialPort
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer2_Tick(Nothing, Nothing)
    End Sub
    Private Sub FullscreenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FullscreenToolStripMenuItem.Click
        If FullscreenToolStripMenuItem.Checked = False Then
            Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
            Me.WindowState = FormWindowState.Maximized
            Me.TopMost = True
            Me.FullscreenToolStripMenuItem.Checked = True
        Else
            Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable ' You can change this to the border style that you are currently using
            Me.WindowState = FormWindowState.Normal
            Me.TopMost = False
            Me.FullscreenToolStripMenuItem.Checked = False
        End If
    End Sub


    Private Sub PortSelection(ByVal sender As Object, ByVal e As EventArgs) Handles COMPortsBox.SelectedIndexChanged
        RTSLamp.BackColor = Color.Gray
        DTRLamp.BackColor = Color.Gray
        If COMPort.IsOpen Then
            COMPort.RtsEnable = False
            COMPort.DtrEnable = False
            ClosePort()
            ' NOTE. Because of the use of Invoke, the port should normally be closed from a different thread, see
            '   MaxiTesterClosing, but to ensure that the following delay works, this is not done in this case.
            ' Empty the message queue before the UI thread goes to sleep to update the modem lamps and the ComboBox.
            Application.DoEvents()
            Sleep(200)                      ' Wait 0.2 second for port to close as this does not happen immediately.
        End If
        COMPort.PortName = COMPortsBox.Text
        COMPort.BaudRate = 9600            ' Default for Max-i: 19200 bit/s, 8 data bits, no parity, 1 stop bit
        COMPort.WriteTimeout = 2000         ' Max time to wait for CTS = 2 sec.
        ' COMPort.ReadBufferSize = 16384      Necessary buffer size for 16C950 UART at 921.6 kbit/s
        Try
            COMPort.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        BaudRateBox.Text = COMPort.BaudRate.ToString
        BitsBox.Text = COMPort.DataBits.ToString
        ParityBox.Text = COMPort.Parity.ToString
        FlowControlBox.Text = COMPort.Handshake.ToString
        If COMPort.IsOpen Then
            COMPort.RtsEnable = True
            RTSLamp.BackColor = Color.LightGreen
            COMPort.DtrEnable = True
            DTRLamp.BackColor = Color.LightGreen
        End If
    End Sub
    Private Sub MaxiTesterLoad(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        For Each COMString As String In My.Computer.Ports.SerialPortNames ' Load all available COM ports.
            COMPortsBox.Items.Add(COMString)
            'COMPortsBox.SelectedIndex = 0
        Next
        COMPortsBox.Sorted = True
        BaudRateBox.Items.Add("110")
        BaudRateBox.Items.Add("300")
        BaudRateBox.Items.Add("600")
        BaudRateBox.Items.Add("1200")
        BaudRateBox.Items.Add("1800")
        BaudRateBox.Items.Add("2400")
        BaudRateBox.Items.Add("4800")
        BaudRateBox.Items.Add("7200")
        BaudRateBox.Items.Add("9600")
        BaudRateBox.Items.Add("14400")
        BaudRateBox.Items.Add("19200")      ' Min. FIFO size 3 Bytes (8030 or 8530)
        BaudRateBox.Items.Add("38400")
        BaudRateBox.Items.Add("57600")      ' Min. FIFO size 8 bytes
        BaudRateBox.Items.Add("115200")     ' Min. FIFO size 16 bytes (16C550)
        BaudRateBox.Items.Add("230400")     ' Min. FIFO size 32 bytes (16C650)
        BaudRateBox.Items.Add("460800")     ' Min. FIFO size 64 bytes (16C750)
        BaudRateBox.Items.Add("921600")     ' Min. FIFO size 128 bytes (16C850 or 16C950)
        BaudRateBox.SelectedIndex = 8
        BitsBox.Items.Add("5")
        BitsBox.Items.Add("6")
        BitsBox.Items.Add("7")
        BitsBox.Items.Add("8")
        ParityBox.Items.Add("None")
        ParityBox.Items.Add("Odd")
        ParityBox.Items.Add("Even")
        ParityBox.Items.Add("Mark")         ' Leaves the parity bit set to 1
        ParityBox.Items.Add("Space")        ' Leaves the parity bit set to 0
        FlowControlBox.Items.Add("None")
        FlowControlBox.Items.Add("RequestToSend")
        FlowControlBox.Items.Add("RequestToSendXOnXOff")
        FlowControlBox.Items.Add("XOnXOff")
        RTSLamp.BackColor = Color.Gray
        DTRLamp.BackColor = Color.Gray
        CTSLamp.BackColor = Color.Gray
        DSRLamp.BackColor = Color.Gray
    End Sub
    Private Sub ClosePort()
        If COMPort.IsOpen Then COMPort.Close()
    End Sub

    ' This subroutine is activated when the form is closed. It closes the COM port. Without such a close command,
    '   the garbage collector may close the COM port while it is still in use!

    Private Sub MaxiTesterClosing(ByVal sender As Object, ByVal e As ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If MessageBox.Show("Do you really want to close the window", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            e.Cancel = True
        Else
            ' Close COM port on a new thread when the form is terminated with [X]
            Dim t As New Thread(AddressOf ClosePort)
            t.Start()
        End If
    End Sub

    ' This subroutine handles a change in Baud Rate.

    Private Sub BaudRateSelection(ByVal sender As Object, ByVal e As EventArgs) Handles BaudRateBox.SelectedIndexChanged
        COMPort.BaudRate = CInt(BaudRateBox.Text)
    End Sub

    ' This subroutine handles a change in number of data bits

    Private Sub DataBitsSelection(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BitsBox.SelectedIndexChanged
        COMPort.DataBits = CInt(BitsBox.Text)
    End Sub

    ' This subroutine handles a change in the parity.

    Private Sub ParitySelection(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ParityBox.SelectedIndexChanged
        COMPort.Parity = CType([Enum].Parse(GetType(Parity), ParityBox.Text), Parity)
    End Sub

    ' This subroutine handles a change in the flow control (handshake).

    Private Sub SoftwareFlowControlSelection(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlowControlBox.SelectedIndexChanged
        COMPort.Handshake = CType([Enum].Parse(GetType(Handshake), FlowControlBox.Text), Handshake)
    End Sub

    ' This subroutine updates the modem control signal lamps

    Private Sub ModemLamps(ByVal sender As Object, ByVal e As SerialPinChangedEventArgs) Handles COMPort.PinChanged
        If COMPort.DsrHolding Then
            DSRLamp.BackColor = Color.LightGreen
        Else
            DSRLamp.BackColor = Color.Gray
        End If
        If COMPort.CtsHolding Then
            CTSLamp.BackColor = Color.LightGreen
        Else
            CTSLamp.BackColor = Color.Gray
        End If
    End Sub

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim returnstring As String
        Dim FileName As String
        Dim erg As Integer
        returnstring = Space(127)
        'Replace d:\my box\MyMovie.avi with the AVI file you want to play
        FileName = "C:\Users\nzx\Videos\line0.avi"
        FileName = Chr(34) & FileName & Chr(34)
        erg = mciSendString("open" & FileName & " type mpegvideo alias movie", 0, 0, 0)
        erg = mciSendString("set video time format ms", returnstring, 127, 0)
        erg = mciSendString("play video from 0", returnstring, 127, 0)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick


        Dim hour As Integer = DateTime.Now.Hour
        Dim min As Integer = DateTime.Now.Minute
        Dim sec As Integer = DateTime.Now.Second
        Dim day As Integer = DateTime.Now.Day
        Dim mon As Integer = DateTime.Now.Month
        Dim yr As Integer = DateTime.Now.Year


        Label7.Text = hour & " : " & min & " : " & sec & "        " & day & "/" & mon & "/" & yr
    End Sub

    Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter

    End Sub

    Private Sub GroupBox4_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Sub ReadExcel()
        Dim ef As New ExcelFile
        Dim rw As Integer
        ' Loads Excel file.
        ef.LoadXls("ads.xls")
        news.Items.Clear()
        advert.Items.Clear()
        news.Items.Add(" Id  | Advertiser | Info |  Publish?" & vbCrLf)
        advert.Items.Add(" Id  | Advertiser | Advertisement | Publish?" & vbCrLf)
        ' Selects first worksheet.
        Dim ws As ExcelWorksheet = ef.Worksheets(0)
        rw = ws.Rows.Count
        For i As Integer = 1 To rw - 1
            news.Items.Add(ws.Cells(i, 1).Value.ToString() & " | " & ws.Cells(i, 3).Value.ToString() & " | " & ws.Cells(i, 4).Value.ToString() & " | " & ws.Cells(i, 5).Value.ToString() & vbCrLf)
        Next
        ws = ef.Worksheets(1)
        rw = ws.Rows.Count
        For i As Integer = 1 To rw - 1
            advert.Items.Add(ws.Cells(i, 1).Value.ToString() & " | " & ws.Cells(i, 3).Value.ToString() & " | " & ws.Cells(i, 4).Value.ToString() & " | " & ws.Cells(i, 5).Value.ToString() & vbCrLf)
        Next
    End Sub
    Private Sub SendSerialData(ByVal data As String)
        ' Send strings to a serial port. 
        If COMPort.IsOpen Then
            COMPort.WriteLine(data & vbCrLf)
        End If

    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        ReadExcel()
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles advert.SelectedIndexChanged

    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles npub.Click
        Timer2.Enabled = False
        Dim data As String
        Dim k As Integer
        Dim i As Integer
        i = news.SelectedIndex
        If i > 0 Then
            data = news.Items(i)
            k = data.IndexOf("Not Publish")
            If k > 0 Then
                data = data.Substring(0, k - 2)
                SendSerialData(data)
                Dim ef As New ExcelFile
                ' Loads Excel file.
                ef.LoadXls("ads.xls")
                ' Selects first worksheet.
                Dim ws As ExcelWorksheet = ef.Worksheets(0)
                ws.Cells(i, 5).Value = "Publish"
                ' ef.SaveXls("ads1.xls")

                ef.SaveXls("ads.xls")
            End If

        End If

        Timer2_Tick(Nothing, Nothing)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles apub.Click
        Timer2.Enabled = False
        Dim data As String
        Dim k As Integer
        Dim i As Integer
        i = advert.SelectedIndex
        If i > 0 Then
            data = advert.Items(i)
            k = data.IndexOf("Not Publish")
            If k > 0 Then
                data = data.Substring(0, k - 2)
                SendSerialData(data)
                Dim ef As New ExcelFile
                ' Loads Excel file.
                ef.LoadXls("ads.xls")
                ' Selects first worksheet.
                Dim ws As ExcelWorksheet = ef.Worksheets(1)
                ws.Cells(i, 5).Value = "Publish"
                ' ef.SaveXls("ads1.xls")

                ef.SaveXls("ads.xls")
            End If

        End If

        Timer2_Tick(Nothing, Nothing)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ndep.Click
        Timer2.Enabled = False

        Dim i As Integer
        Dim data As String
        Dim k As Integer
        i = news.SelectedIndex
        If i > 0 Then
            data = news.Items(i)
            k = data.IndexOf("Publish")
            If k > 0 Then
                data = "XX | " & data.Substring(5, k - 2)

                SendSerialData(data)

                Dim ef As New ExcelFile
                ' Loads Excel file.
                ef.LoadXls("ads.xls")
                ' Selects first worksheet.
                Dim ws As ExcelWorksheet = ef.Worksheets(0)
                ws.Cells(i, 5).Value = "Not Publish"
                ' ef.SaveXls("ads1.xls")

                ef.SaveXls("ads.xls")
            End If
        End If
        Timer2_Tick(Nothing, Nothing)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles adep.Click
        Timer2.Enabled = False

        Dim i As Integer
        Dim data As String
        Dim k As Integer
        i = advert.SelectedIndex
        If i > 0 Then
            data = advert.Items(i)
            k = data.IndexOf("Publish")
            If k > 0 Then
                data = "XX | " & data.Substring(5, k - 2)

                SendSerialData(data)
                Dim ef As New ExcelFile
                ' Loads Excel file.
                ef.LoadXls("ads.xls")
                ' Selects first worksheet.
                Dim ws As ExcelWorksheet = ef.Worksheets(1)
                ws.Cells(i, 5).Value = "Not Publish"
                ' ef.SaveXls("ads1.xls")

                ef.SaveXls("ads.xls")
            End If
        End If
        Timer2_Tick(Nothing, Nothing)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nrem.Click
        Timer2.Enabled = False

        Dim i As Integer
        i = news.SelectedIndex
        If i > 0 Then
            Dim ef As New ExcelFile
            ' Loads Excel file.
            ef.LoadXls("ads.xls")
            ' Selects first worksheet.
            Dim ws As ExcelWorksheet = ef.Worksheets(0)
            ws.Rows(i).Delete()
            ' ef.SaveXls("ads1.xls")

            ef.SaveXls("ads.xls")
        End If

        Timer2_Tick(Nothing, Nothing)
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles arem.Click
        Timer2.Enabled = False

        Dim i As Integer
        i = advert.SelectedIndex
        If i > 0 Then
            Dim ef As New ExcelFile
            ' Loads Excel file.
            ef.LoadXls("ads.xls")
            ' Selects first worksheet.
            Dim ws As ExcelWorksheet = ef.Worksheets(1)
            ws.Rows(i).Delete()
            ' ef.SaveXls("ads1.xls")

            ef.SaveXls("ads.xls")
        End If

        Timer2_Tick(Nothing, Nothing)
    End Sub
End Class
